package myky.com.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView

import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter

import myky.com.doctorapp.R
import myky.com.doctorapp.entity.SearchHistoryItem

class SearchResultAdapter(private val context: Context, datas: List<SearchHistoryItem>) : TagAdapter<SearchHistoryItem>(datas) {
    private var onTagClicListener: OnTagClicListener? = null

    fun setOnTagClicListener(onTagClicListener: OnTagClicListener) {
        this.onTagClicListener = onTagClicListener
    }

    interface OnTagClicListener {
        fun onTagClick(seachContent: String)

        fun deleteCallBack(id: Int, postion: Int)
    }


    override fun getView(parent: FlowLayout, position: Int, s: SearchHistoryItem): View {
        val view = LayoutInflater.from(context).inflate(R.layout.item_search_history, parent, false)
        val textView = view.findViewById<TextView>(R.id.temp)

        val imageViewiew = view.findViewById<ImageView>(R.id.deleteIcon)
        val deleteLayout = view.findViewById<RelativeLayout>(R.id.deleteLayout)

        textView.text = s.logcontent


        view.setOnLongClickListener {
            val childCount = parent.childCount

            for (i in 0 until childCount) {
                val viewById = parent.getChildAt(i).findViewById<View>(R.id.deleteIcon)
                viewById.visibility = View.GONE
                if (position == i) {
                    viewById.visibility = View.VISIBLE
                }
            }
            true
        }
        deleteLayout.setOnClickListener {
            if (imageViewiew.visibility == View.VISIBLE) {
                onTagClicListener!!.deleteCallBack(s.id, position)
            }
        }
        view.setOnClickListener {
            val childCount = parent.childCount

            for (i in 0 until childCount) {
                val viewById = parent.getChildAt(i).findViewById<View>(R.id.deleteIcon)
                viewById.visibility = View.GONE
            }
            onTagClicListener!!.onTagClick(s.logcontent)
        }

        return view
    }
}
